package controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @Auther: qinghuan
 * @Date: 2022/3/6 13:39
 * @Description:
 */
@Controller
public class IndexController{

    @RequestMapping("/index")
    public ModelAndView handleRequest(HttpServletRequest request,
                                      HttpServletResponse response) throws Exception {
        ModelAndView mav = new ModelAndView("index");
        mav.addObject("message", "Hello Spring MVC");
        return mav;
    }

    @RequestMapping("/jump")
    public ModelAndView jump() {
        ModelAndView mav = new ModelAndView("redirect:/index");
        return mav;
    }

    @RequestMapping("/check")
    public ModelAndView check(HttpSession session) {
        Integer integer = (Integer) session.getAttribute("count");
        if (integer == null) {
            integer = 0;
        }
        integer++;
        session.setAttribute("count", integer);
        ModelAndView mav = new ModelAndView("check");
        return mav;
    }

    @RequestMapping("/clear")
    public ModelAndView clear(HttpSession session) {
        session.setAttribute("count", -1);
        ModelAndView modelAndView = new ModelAndView("redirect:check");
        return modelAndView;
    }

}
